package com.author.menubar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import com.author.surface.Surface;
import com.author.surface.TextPane;
import com.author.surface.TextPanel;

public class InsertMenu extends JMenu {
	/** Header */
	private JMenuItem	insertheaderMenuItem	= new JMenuItem("Header");

	/** Footer */
	private JMenuItem	insertfooterMenuItem	= new JMenuItem("Footer");

	/** Table */
	private JMenuItem	inserttableMenuItem		= new JMenuItem("Table");

	/** Horizontal Rule */
	private JMenuItem	inserthrMenuItem		= new JMenuItem(
														"Horizontal Rule");

	/** Pictures */
	private JMenuItem	insertpictureMenuItem	= new JMenuItem("Picture");

	/** Object */
	private JMenuItem	insertobjectMenuItem	= new JMenuItem("Object");

	public InsertMenu(String menuname) {
		super(menuname);

		/*
		 * Add menus for inserting and removing header and footer from the text
		 * pane
		 */
		this.add(insertheaderMenuItem);
		this.add(insertfooterMenuItem);

		/* Make header and footer menu items responsive to user clicks */
		this.insertheaderMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent ae) {
				/*
				 * Get application surface, then text panel and then go to the
				 * text pane to insert header
				 */
				Surface s = (Surface) Surface.getAppSurface();
				TextPanel tpanel = (TextPanel) s.getTextPanel();
				TextPane tpane = (TextPane) tpanel.getTextPane();

				/* Create header inside text pane */
				if (tpane.getHeader() == null) {
					tpane.createHeader();
					return;
				}

				/* Remove header if a header already exists */
				tpane.removeHeader();
			}
		});

		/* Add menu item separator */
		this.addSeparator();

		/* Add table menu item for table insertion */
		this.add(inserttableMenuItem);
		inserttableMenuItem.setAccelerator(KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_F12, java.awt.Event.CTRL_MASK));

		/* Add HR menu item */
		this.add(inserthrMenuItem);

		/* Add picture menu item */
		this.add(insertpictureMenuItem);
		insertpictureMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent ae) {
				/* Create a new picture frame and prompt the user to select file */
				JFrame pictureFrame = new InsertPictureFrame();
			}
		});

		/* Add object */
		this.add(insertobjectMenuItem);
	}

}
